package de.tabuma.lib.webservice.wsdl;

import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:16:26 $
 */
public class MessageFacade {

    private static final Logger log = Logger.getLogger(MessageFacade.class.getName());

    public static String getMessage(Hashtable<String,Class> methodTable) {
        String str = "";
        Enumeration<String> keys = methodTable.keys();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            Class clazz = methodTable.get(key);
            for(int i=0;i<clazz.getDeclaredMethods().length;i++) {
                Method method = clazz.getDeclaredMethods()[i];
                str += getMessage(key, method);
            }
        }
        return str;
    }

    public static String getMessage(String name, Method method) {
        String str = "";
        str += "<message name=\""+name+"_"+method.getName()+"_input\">\n";
        if(method.getParameterTypes().length>0) {
            str += getPart("Request", name, method);
        }
        str += "</message>";
        if(!method.getReturnType().getName().equals("void")) {
            str += "<message name=\""+name+"_"+method.getName()+"_output\">\n";
            if(!method.getReturnType().getClass().getName().equals("void")) {
                str += getPart("Response", name, method);
            }
            str += "</message>";
            str += "<message name=\""+name+"_"+method.getName()+"_exception\">\n";
            if(!method.getReturnType().getClass().getName().equals("void")) {
                str += getPart("Exception", name, method);
            }
            str += "</message>";
        }
        return str;
    }

    public static String getPart(String style, String name, Method method) {
        String str = "";
        str += "<part name=\""+name+"_"+method.getName()+"_object\" element=\"tns:"+style+"_"+name+"_"+method.getName()+"\"/>\n";
        return str;
    }
}
